﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;

namespace IronTrishul
{
    public partial class WebScript : Form
    {
        int lastPos = 0;
        StringBuilder sb = new StringBuilder();
        List<string> statements = new List<string>();
        StringBuilder statementLine = new StringBuilder();
        public string fileName { get; set; }
        static  Action<String> commandhandler;

        public WebScript()
        {
            InitializeComponent();
            commandhandler = new Action<string>(AppendConsole);
        }

        private void textBoxScript_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                string lastStateMent = textBoxScript.Text;
                ExecuteCommand(lastStateMent);
            }
            else
            {

            }

        }
        private void AppendConsole(string line)
        {
            textBoxConsole.AppendText(Environment.NewLine + ">>" + line);
        }

        public static void WriteLine(string line)
        {
            commandhandler.Invoke(line);
        }
        private void WebScript_Load(object sender, EventArgs e)
        {
            sb = new StringBuilder();
            //DataExchange();
        }

        private void clearToolStripMenuItem_Click(object sender, EventArgs e)
        {
            sb = new StringBuilder();
            //DataExchange();
        }

        private void textBoxScript_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBoxConsole_TextChanged(object sender, EventArgs e)
        {

        }

        private void CommandResult(IAsyncResult ec)
        {
            object o = ec.AsyncState;
            sb.Append("Command Executed successfully");
            //DataExchange();
        }

        private void ExecuteCommand(string command)
        {
            try
            {
                Trace.WriteLine("Trying line --" + command);
                WindowProgram.RunProgram(command);
            }
            catch (Exception dcx)
            {
                Trace.WriteLine("Error -- " + dcx.ToString());
                sb.Append(Environment.NewLine + "Band Command of File Name, Message - >" + dcx.Message);
                WriteLine(Environment.NewLine + ">>Band Command of File Name, Message - >" + dcx.Message);
            }
            finally
            {
                sb.Append(Environment.NewLine + "Command Executed successfully.");
                //DataExchange();
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void browseF3ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                fileName = openFileDialog1.FileName;
                sb.Append(File.ReadAllText(openFileDialog1.FileName));
                //DataExchange();
            }
        }

        private void runF5ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ExecuteCommand(textBoxScript.Text);
        }

        private void saveF2ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                File.AppendAllText(saveFileDialog1.FileName, sb.ToString());
            }
        }


    }
}
